package com.zhangxi.websqlit.contact.service.impl;

import com.zhangxi.websqlit.common.util.DataUtil;
import com.zhangxi.websqlit.common.util.FileUtil;
import com.zhangxi.websqlit.contact.control.ChartWebSocketServer;
import com.zhangxi.websqlit.contact.entity.AddFriendsEntity;
import com.zhangxi.websqlit.contact.entity.SearchChatEntity;
import com.zhangxi.websqlit.contact.repository.ContactDao;
import com.zhangxi.websqlit.contact.service.ContactService;
import com.zhangxi.websqlit.mybatis.entity.ChatReportEntity;
import com.zhangxi.websqlit.mybatis.entity.PtContactEnity;
import com.zhangxi.websqlit.user.entity.UserSearchEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ContactServiceImpl implements ContactService {
    @Resource
    private ContactDao contactDao;

    @Override
    public List<PtContactEnity> queryContactListById(UserSearchEntity userSearchEntity) {

        List<PtContactEnity> list = contactDao.queryContactListById(userSearchEntity);
        String startStr = "";
        Integer index;
        for(PtContactEnity item:list){
            startStr = DataUtil.getChCharInit(item.getRemark());
            item.setSection(startStr);
        }
        
        return list;
    }

    @Override
    public List<PtContactEnity> queryMessageListById(UserSearchEntity userSearchEntity) {
        return contactDao.queryMessageListById(userSearchEntity);
    }
    @Override
    public void updateRemarkById(PtContactEnity contactEnity) {
         contactDao.updateRemarkById(contactEnity);
    }

    @Override
    public void addFriendsById(AddFriendsEntity friendsEntity) {
        //为当前用户增加好朋
        contactDao.addFriendsById(friendsEntity.getUserUuid(),friendsEntity.getContactUuid(),friendsEntity.getContactRemark());
        //为好朋的添厍好朋
        contactDao.addFriendsById(friendsEntity.getContactUuid(),friendsEntity.getUserUuid(),friendsEntity.getUserRemark());
    }

    @Override
    public List<ChatReportEntity> queryChartReport(SearchChatEntity search) {
        List<ChatReportEntity> list = contactDao.queryChartReport(search);
        contactDao.updateLocalContactReadState(search);
        List<ChatReportEntity> outList = new ArrayList<>();
        ChatReportEntity dateChat = null;
        for(ChatReportEntity item:list){
            if(!item.equals(dateChat)){
                dateChat = new ChatReportEntity(item.getModtime1());
                outList.add(dateChat);
            }
            outList.add(item);
        }
        return outList;
    }

    @Override
    public void addChartReport(ChatReportEntity chatReportEntity) {
        chatReportEntity.setChatUuid(FileUtil.getUUID());
        contactDao.addLocalChartReport(chatReportEntity);
        chatReportEntity.setChatUuid(FileUtil.getUUID());
        contactDao.addRemoteChartReport(chatReportEntity);
        boolean rst = ChartWebSocketServer.sendInfo(chatReportEntity.getChatContent(), chatReportEntity.getContactUuid(), chatReportEntity.getUserUuid());
        if(!rst){
            contactDao.updateRemoteContactNewState(chatReportEntity);
        }

    }

}
